import { handleSearchLoading } from "@userList/search";
import { IMOptionsType } from "@/types/dict";
import { handleSearchTime } from "@userList/search";
import {
  columns,
  searchList,
  tableActiveKey,
  tabPower,
  defaultColumns,
  defaultSearchList,
  resetFormState,
  drawerFormState,
  defaultTabsActiveKey,
  queryParams,
} from "./store";
import { fetchIncomeSearchOptions, fetchPaySearchOptions } from "./utils";
import { refreshTable } from ".";
const searchLoading = ref(false);
const resetLoading = ref(false);
export const searchConfig = () => {
  const defineSearch = {
    okText: getProxyT("base.query"),
    cancelText: getProxyT("base.reset"),
    searchLoading,
    resetLoading,
    refreshTableData: async (searchParams: any, type: any) => {
      handleSearchTime(["start_time", "end_time"], searchParams, "YYYY-MM-DD");
      searchParams.order_type =
        searchParams.order_type + "" === "Infinity"
          ? undefined
          : searchParams.order_type;
      searchParams.pay_platform_id =
        searchParams.pay_platform_id + "" === "Infinity"
          ? undefined
          : searchParams.pay_platform_id;
      if (type === 1) {
        initObjAttr(defaultTabsActiveKey, [0, 0]);
        searchParams.status = undefined;
      }
      handleSearchLoading(true, type, defineSearch);
      await refreshTable({ ...queryParams, ...searchParams });
      handleSearchLoading(false, type, defineSearch);
    },
    tabsSlot: {
      activeKey: tableActiveKey,
      on: {
        change: async (tableActiveKey: any) => {
          initObjAttr(columns, defaultColumns[tableActiveKey]);
          searchList.value = defaultSearchList[tableActiveKey];
          if (tableActiveKey === 0) fetchIncomeSearchOptions();
          if (tableActiveKey === 1) fetchPaySearchOptions();
          resetFormState(drawerFormState);
          defaultTabsActiveKey[tableActiveKey] = 0;
          refreshTable();
        },
      },
      options: ([] as IMOptionsType[])
        .concat(
          tabPower[0]
            ? {
                label: getProxyT("tradeDetail0"),
                value: 0,
              }
            : []
        )
        .concat(
          tabPower[1]
            ? {
                label: getProxyT("tradeDetail1"),
                value: 1,
              }
            : []
        ),
    },
    searchList,
  };
  return defineSearch;
};
